package com.ef.web.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.ConfirmMessage;
import com.ef.constant.StatusType;
import com.ef.domain.School;
import com.ef.domain.Teacher;
import com.ef.service.SchoolService;
import com.ef.service.TeacherService;
import com.ef.util.CustomExample;

public class TeacherAction extends BaseAction<Teacher> {

	private static final long serialVersionUID = 1L;
	private TeacherService teacherService;
	private Map<String, String> schoolList;
	protected final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(TeacherAction.class);

	private void fetchListData() {
		Map<String, String> schoolMap = new LinkedHashMap<String, String>();
		List<School> schools = schoolService.findAll();
		for (School s : schools) {
			if (s.getStatus() == StatusType.VALID)
				schoolMap.put(s.getId().toString().trim(), s.getName().trim());
		}
		schoolList = schoolMap;
	}

	@SuppressWarnings("serial")
	public String list() {
		final School school = this.getSessionUser().getSchool();
		this.listResult = teacherService.findByExample(
				new CustomExample<Teacher>(this.getModel()) {
					public void appendCondition(Criteria criteria) {
						if (school!=null){
							criteria.add(Restrictions.eq("school", school));
						}
						this.excludeProperty("client");
						this.excludeProperty("foreign");
						this.enableLike(MatchMode.ANYWHERE);
					}
				}, this.getStartIndex(), this.getPageSize());
		return LIST;
	}

	private SchoolService schoolService;

	public String edit() {
		fetchListData();

		return EDIT;
	}
 

	public String save() {
		Teacher teacher = this.getModel();
		teacher.setUpdateTime(new Date());
		teacher.setUpdateUser(getSessionUser());
		if(teacher.getSchool()==null||teacher.getSchool().getId()==null){
			teacher.setSchool(null);
		}
		teacherService.saveOrUpdate(teacher);
		this.setResult(ConfirmMessage.TEACHER);
		fetchListData();
		return EDIT;
	}

	public String delete() {
		String ids = getRequest().getParameter("ids");
		String[] id = ids.split(",");
		for (String i : id) {
			teacherService.delete(teacherService.findById(Long.valueOf(i)));
		}

		return DELETE;
	}

	public TeacherService getTeacherService() {
		return teacherService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public String autoCompleteAjax() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/plain;charset=GBK ");
		PrintWriter out = null;
		try {
			String q = request.getParameter("q");
			String limit = request.getParameter("limit");
			School school = this.getSessionUser().getSchool();
			List<Teacher> list  = null;
			if (school != null) {
				list = teacherService.findByNameInSchool(q, school.getId(), Integer
						.valueOf(limit));
			} else {
				list = teacherService.findByName(q, Integer
						.valueOf(limit));
			}
			// {id:id,name:name}
			String responseStr = "";
			// System.out.println(responseStr);
			out = response.getWriter();
			for (Teacher key : list) {
				responseStr = ("{id:") + (key.getId()) + (",") + ("name:\"")
						+ (key.getName()) + ("\"}");
				// System.out.println(responseStr);
				out.println(responseStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (Exception e) {

				}
			}
		}
		return null;
	}

	public Map<String, String> getSchoolList() {
		return schoolList;
	}

	public void setSchoolList(Map<String, String> schoolList) {
		this.schoolList = schoolList;
	}

	public SchoolService getSchoolService() {
		return schoolService;
	}

	public void setSchoolService(SchoolService schoolService) {
		this.schoolService = schoolService;
	}
 
}
